//管理员路由


var express = require('express');
var router = express.Router();
const multiparty = require("multiparty");
const path = require('path');
const jwt = require("jsonwebtoken");
const {secretKey} = require('../token');

let adminService=require("../src/services/adminServices")
/* GET users listing. */
router.post("/upload", function (req, res) {
    var form = new multiparty.Form({ uploadDir: "./public/images" });
    form.parse(req, function (err, fields, files) {
      if (err) {
        res.send(err);
      } else {
        console.log(files.file);
        var p = files.file[0].path.substring(
          files.file[0].path.indexOf("images")
        );
        res.send({filename:path.basename(p)});
      }
    });
  });


  router.post('/login',async function(req,res){
    let {adminName,adminPwd} = req.body;
    let data = await adminService.findByUsernameAndPwd(adminName, adminPwd);
    console.log("data",data.length);
    if(data.length > 0){
      // let authToken = jwt.sign({data:data[0]}, secretKey);
      // req.session.user = data[0];
      // res.send({status:1,token:authToken});
      res.send({status:1});
    }else{
      res.send({status:0});
    }
  });
  
  router.get('/getToken',function(req,res){
    // 获取到客户端发送过来的 token
    const token = req.get('Authorization');
    // 将编码后的 token 再转换回来
    const user = jwt.verify(token.split(" ")[1], secretKey);
    res.send(user);
  })
module.exports = router;